<template>
  <div id="manageQuestionView">
    <a-table :ref="tableRef" :columns="columns" :data="dataList" :pagination="{
      showTotal: true,
      pageSize: searchParams.pageSize,
      current: searchParams.current,
      total,
    }" @page-change="onPageChange">
      <!-- 自定义内容列 -->
      <template #content="{ record }">
        <div class="ellipsed-column">
          {{ record.content }}
        </div>
      </template>
      <!-- 自定义答案列 -->
      <template #answer="{ record }">
        <div class="ellipsed-column">
          {{ record.answer }}
        </div>
      </template>
      <!-- 自定义标签列 -->
      <template #tags="{ record }">
        <div style="width: 110px;">
          <a-tag v-for="tag in JSON.parse(record.tags)" :key="tag" :checkable="true" :default-checked="true"
            style="margin-left: 10px;" color="green">
            {{ tag }}
          </a-tag>
        </div>
      </template>
      <template #createTime="{ record }">
        <div style="width: 100px;">
          {{ moment(record.createTime).format("YYYY-MM-DD") }}
        </div>
      </template>
      <!-- 自定义判题配置列 -->
      <template #judgeConfig="{ record }">
        <div class="judge-config" style="width: 130px;">
          <div v-for="(value, key) in formatJudgeConfig(record.judgeConfig)" :key="key">
            <strong>{{ key }}:</strong> {{ value }}
          </div>
        </div>
      </template>
      <!-- 自定义判题用例列 -->
      <template #judgeCase="{ record }">
        <div class="test-case">
          <div v-for="(testCase, index) in parseTestCases(record.judgeCase)" :key="index" class="test-case-item">
            <span>输入:</span>
            <pre class="inline">{{ formatTestCase(testCase.input) }}</pre>
            <br />
            <span>输出:</span>
            <pre class="inline">{{ testCase.output }}</pre>
          </div>
        </div>
      </template>
      <template #optional="{ record }">
        <a-space>
          <a-button type="primary" @click="doUpdate(record)"> 修改</a-button>
          <a-button status="danger" @click="doDelete(record)">删除</a-button>
        </a-space>
      </template>

    </a-table>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref, watchEffect } from "vue";
import {
  Page_Question_,
  Question,
  QuestionControllerService,
} from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import * as querystring from "querystring";
import { useRouter } from "vue-router";
import moment from "moment";

const tableRef = ref();

const dataList = ref([]);
const total = ref(0);
const searchParams = ref({
  pageSize: 10,
  current: 1,
});

const loadData = async () => {
  const res = await QuestionControllerService.listQuestionByPageUsingPost(
    searchParams.value
  );
  if (res.code === 0) {
    console.log('res--manage', res.data);

    dataList.value = res.data.records;
    total.value = res.data.total;
  } else {
    message.error("加载失败，" + res.message);
  }
};

/**
 * 监听 searchParams 变量，改变时触发页面的重新加载
 */
watchEffect(() => {
  loadData();
});

/**
 * 页面加载时，请求数据
 */
onMounted(() => {
  loadData();
});

// {id: "1", title: "A+ D", content: "新的题目内容", tags: "["二叉树"]", answer: "新的答案", submitNum: 0,…}

const columns = [
  // {
  //   title: "id",
  //   dataIndex: "id",
  // },
  {
    title: "标题",
    dataIndex: "title",
  },
  {
    title: "内容",
    slotName: "content",
  },
  {
    title: "标签",
    slotName: "tags",
  },
  {
    title: "答案",
    slotName: "answer",
  },
  {
    title: "提交数",
    dataIndex: "submitNum",
  },
  {
    title: "通过数",
    dataIndex: "acceptedNum",
  },
  {
    title: "判题配置",
    slotName: "judgeConfig",
  },
  {
    title: "判题用例",
    slotName: "judgeCase",
  },
  {
    title: "用户",
    dataIndex: "user.userName",
  },
  {
    title: "创建时间",
    slotName: "createTime",
  },
  {
    title: "操作",
    slotName: "optional",
  },
];

const onPageChange = (page: number) => {
  searchParams.value = {
    ...searchParams.value,
    current: page,
  };
};

const doDelete = async (question: Question) => {
  const res = await QuestionControllerService.deleteQuestionUsingPost({
    id: question.id,
  });
  if (res.code === 0) {
    message.success("删除成功");
    loadData();
  } else {
    message.error("删除失败");
  }
};

const router = useRouter();

const doUpdate = (question: Question) => {
  router.push({
    path: "/update/question",
    query: {
      id: question.id,
    },
  });
};

// 将 judgeConfig 字符串解析为对象并格式化为特定的显示格式
const formatJudgeConfig = (judgeConfig: string) => {
  try {
    const config = JSON.parse(judgeConfig);
    return {
      "时间限制": `${config.timeLimit} ms`,
      "空间限制": `${config.memoryLimit} KB`,
      "堆限制": `${config.stackLimit} KB`,
    };
  } catch (e) {
    return {};
  }
};

// 解析判题用例的 JSON 字符串
const parseTestCases = (judgeCase: string) => {
  try {
    return JSON.parse(judgeCase);
  } catch (e) {
    return [];
  }
};

// 格式化判题用例输入
const formatTestCase = (input: string) => {
  return input
    .split(', ')
    .map(line => line.trim())
    .join('\n');
};
</script>

<style scoped>
#manageQuestionView .ellipsed-column {
  width: 150px;
  height: 50px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.test-case {
  white-space: pre-wrap;
  word-wrap: break-word;
}

.test-case-item {
  margin-bottom: 8px;
}

.inline {
  display: inline;
  white-space: pre-wrap;
}
</style>
